using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_HeroInfoOverview : UIBaseNode
    {

        #region Template Generate,don't modify
        protected partial class UIB_UIN_HeroInfoOverview
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton btnFeatureDetail { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnAttrDetail { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum typeIcon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText text_Name { protected set; get; }
            public UnityEngine.GameObject point { protected set; get; }
            public IQIGame.Onigao.Framework.ExProgressBar expBar { protected set; get; }
            public UnityEngine.GameObject star { protected set; get; }
            public UnityEngine.GameObject feature { protected set; get; }
            public UnityEngine.GameObject talent { protected set; get; }
            public UnityEngine.GameObject skillCell { protected set; get; }
            public UnityEngine.GameObject attMould { protected set; get; }
            public UnityEngine.GameObject homeSkillCell { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum lvCurr { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textLvMax { protected set; get; }
            public UnityEngine.GameObject awakenLv { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum power { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnFeatureDetail", out var __tbv0);
                this.btnFeatureDetail = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnAttrDetail", out var __tbv1);
                this.btnAttrDetail = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("typeIcon", out var __tbv2);
                this.typeIcon = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("text_Name", out var __tbv3);
                this.text_Name = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("point", out var __tbv4);
                this.point = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExProgressBar>("expBar", out var __tbv5);
                this.expBar = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("star", out var __tbv6);
                this.star = __tbv6;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("feature", out var __tbv7);
                this.feature = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("talent", out var __tbv8);
                this.talent = __tbv8;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("skillCell", out var __tbv9);
                this.skillCell = __tbv9;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attMould", out var __tbv10);
                this.attMould = __tbv10;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("homeSkillCell", out var __tbv11);
                this.homeSkillCell = __tbv11;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("lvCurr", out var __tbv12);
                this.lvCurr = __tbv12;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textLvMax", out var __tbv13);
                this.textLvMax = __tbv13;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("awakenLv", out var __tbv14);
                this.awakenLv = __tbv14;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("power", out var __tbv15);
                this.power = __tbv15;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private UIN_CommonTitleValue talent = null;
        private ListScrollAdapter<UIN_HeroInfoAwakenLv> listAwaken = null;
        private ListScrollAdapter<UIN_CommonDarkLight> listStar = null;
        private ListScrollAdapter<UIN_HeroSkillCell> listSkill = null;
        private ListScrollAdapter<UIN_CommonAttribute> listAttr = null;
        private ListScrollAdapter<UIN_HeroSkillCell> listHomeSkill = null;
        private ListScrollAdapter<UIN_CommonDarkLight> listFeature = null;

        private HeroData heroData = null;
        private CfgHero cfgHero = null;
        private UIEntityViewHelper entityHelper = null;
        private List<UD_HereFeature> listDataHeroFreature = null;

        protected UIB_UIN_HeroInfoOverview ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_HeroInfoOverview();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.talent = this.InitChildNode<UIN_CommonTitleValue>(this.ui.talent);

            // this._heroWarehouse = this.InitChildNode<UIN_HeroInfoWarehouse>()

            this.listAwaken = this.InitListScroll<UIN_HeroInfoAwakenLv>(this.ui.awakenLv, this.OnAwakenChanged);
            this.listStar = this.InitListScroll<UIN_CommonDarkLight>(this.ui.star, this.OnStarChanged);
            this.listFeature = this.InitListScroll<UIN_CommonDarkLight>(this.ui.feature, this.OnFeatureChanged);
            this.listSkill = this.InitListScroll<UIN_HeroSkillCell>(this.ui.skillCell, this.OnSkillChanged);
            this.listAttr = this.InitListScroll<UIN_CommonAttribute>(this.ui.attMould, this.OnAttrChanged);
            this.listHomeSkill = this.InitListScroll<UIN_HeroSkillCell>(this.ui.homeSkillCell, this.OnHomeSkillChanged);

            this.ui.btnAttrDetail.onClick.AddListener(this.OnClickBtnShowAttrDetails);
            this.ui.btnFeatureDetail.onClick.AddListener(this.OnClickBtnShowFeatureDetail);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            this.heroData = null;
            this.cfgHero = null;
            this.ClearListFreatureData();
            this.ClearModel();
        }

        private void ClearListFreatureData()
        {
            if (this.listDataHeroFreature != null)
            {
                foreach (var item in this.listDataHeroFreature)
                    item.Put2Pool();
            }
            this.listDataHeroFreature = null;
        }



        public void Show(HeroData heroData)
        {
            this.heroData = heroData;
            this.cfgHero = null;
            if (heroData == null)
                return;
            this.Show();
            int maxLevel = heroData.maxLv;

            this.ui.text_Name.text = HeroModule.Instance.GetHeroName(heroData);
            CfgHero cfgHero = heroData.cfgData;
            this.cfgHero = cfgHero;

            this.ui.typeIcon.SetInt(cfgHero.HeroClass);

            var cfgSkin = heroData.cfgSkin;
            this.ShowModel(cfgSkin.BodyPrefab_fullPath, this.ui.point);

            bool isMaxLv = heroData.level >= maxLevel;
            this.ui.lvCurr.text = string.Format("{0:00}", heroData.level);
            this.ui.textLvMax.text = "/" + maxLevel;

            int expCurr = heroData.exp;
            int expNext = heroData.cfgLv.NextEXP;
            int expDiff = expNext - expCurr;
            this.ui.expBar.SetValue(isMaxLv ? 0 : expCurr, expNext);
            this.ui.expBar.text = APIUIHeroInfo.GetNextExpStr(expDiff, isMaxLv);

            this.listStar.RefillCells(APIUIConst.HeroDefautMaxStar);
            this.ClearListFreatureData();
            this.listDataHeroFreature = HeroModule.Instance.GetHeroFeatureCfgStars(this.heroData.cid, this.heroData.star);
            int cntFeature = this.listDataHeroFreature.Count;
            this.listFeature.RefillCells(cntFeature);

            int awkenMaxLv = heroData.maxAwakenLv;
            this.listAwaken.RefillCells(awkenMaxLv);

            int cntSkill = APIUIHeroInfo.DefaultSkillCount;
            this.listSkill.RefillCells(cntSkill);

            this.ui.power.SetInt(heroData.fightPower);

            var attrs = APIUIHeroInfo.BasicAttributeIds;
            this.listAttr.RefillCells(attrs.Length);

            // TODO 家园目前没技能
            this.listHomeSkill.RefillCells(0);
        }

        private void ShowModel(string assetPath, GameObject parent)
        {
            if (this.entityHelper == null)
                this.entityHelper = UIEntityViewHelper.NewFromPool();
            this.entityHelper.ShowModel(assetPath, parent);
        }

        private void ClearModel()
        {
            this.entityHelper?.Put2Pool();
            this.entityHelper = null;
        }

        void OnAwakenChanged(UIN_HeroInfoAwakenLv item, int index)
        {
            item.Show(index + 1, this.heroData.awakenLevel);
        }

        void OnStarChanged(UIN_CommonDarkLight item, int index)
        {
            int nCurr = index;
            int bigStar = -1;
            if (this.heroData.cfgStar != null)
            {
                bigStar = this.heroData.cfgStar.BigStar - 1;
            }
            bool isLight = (bigStar >= nCurr);
            item.Show(isLight);
        }

        void OnFeatureChanged(UIN_CommonDarkLight item, int index)
        {
            var listFeature = this.listDataHeroFreature;
            var csObj = listFeature[index];
            bool isLight = csObj.isUnlock;
            item.Show(isLight);
        }

        void OnSkillChanged(UIN_HeroSkillCell item, int index)
        {
            int skillId = this.cfgHero.DefaultSkill[index];
            HeroSkill heroSkill = this.heroData.skills[skillId];
            item.Show(heroSkill);
        }

        void OnAttrChanged(UIN_CommonAttribute item, int index)
        {
            var attrCid = APIUIHeroInfo.BasicAttributeIds[index];
            var attrVal = this.heroData.GetAttr(attrCid);
            item.Show(attrCid, attrVal);
        }

        void OnHomeSkillChanged(UIN_HeroSkillCell item, int index)
        {
        }

        void OnClickBtnShowAttrDetails()
        {
            MsgDispatcher.Broadcast(MsgEventType.HeroInfoShowAttrDetails);
        }

        void OnClickBtnShowFeatureDetail()
        {
            this.OpenUI<UIHeroFeatureTips>(UD_HeroFeatureTip.Create(this.heroData));
        }
    }
}
